/**
 * 新闻列表
 * Created by KY on 2017/11/9.
 */
define(['ldfw/core', 'art', 'text!tpl/news/list.tpl', 'comps/ajax', 'ldfw/helper/formatHelper'],
    function (LDFW, art, tpl, ajax) {
        const newsTpl = `{{each $data news index }}<div class="weui-media-box weui-media-box_text" article_no="{{news.article_no}}">
                    <h4 class="weui-media-box__title">{{news.official_art_title}}</h4>
                    <p class="weui-media-box__desc"></p>
                    <ul class="weui-media-box__info">
                        <li class="weui-media-box__info__meta">阅读数</li>
                        <li class="weui-media-box__info__meta">{{news.read_count}}</li>
                        <li class="weui-media-box__info__meta weui-media-box__info__meta_extra">{{$imports.format('date','yyyy-MM-dd',news.publish_date)}}</li>
                    </ul>
                </div>{{/each}}`
        return LDFW.Container.extend({
            tpl: tpl,
            initialize: function () {
                const _this = this
                if (is_custom_skin && is_custom_skin.trim()) {
                    this.model.set('is_custom_skin', is_custom_skin)
                    this.model.set('co_no', co_no)
                }
                this.once('shown', function () {
                    this.loadData()
                })
                this.loadData()
            },
            events:{
              'click #newsList .weui-media-box':'viewNews'
            },
            onCompAdd: function (comp) {
                this.comp = comp
                this.comp.on('pullingDown',()=>{
                    this.loadData()
                })
            },
            loadData(){
                const _this = this
                if(co_no==4433 || co_no ==2233){
                    ajax.post('pfL.13.42', {
                        official_art_type: 3,
                        row_count: -1
                    }).success(function (resp) {
                        _this.$el.find('#newsList').html(art.compile(newsTpl)(resp.result))
                        setTimeout(()=> {
                            _this.comp && _this.comp.forceUpdate()
                    }, 1000)
                    })
                }else{
                    ajax.post('pfL.13.24', {
                        official_art_type: 3,
                        row_count: -1
                    }).success(function (resp) {
                        _this.$el.find('#newsList').html(art.compile(newsTpl)(resp.result))
                        setTimeout(()=> {
                            _this.comp && _this.comp.forceUpdate()
                    }, 1000)
                    })
                }

            },
            viewNews(event){
                let articleNo = $(event.currentTarget).attr('article_no')
                LDFW.navigate('/news/'+articleNo)
            }
        })
    })